﻿#region USING STATEMENTS
using System;
using Microsoft.Xna.Framework;
using Fusion.Management;
using Microsoft.Xna.Framework.Input;
#endregion

#region CODE BODY
namespace Fusion.Interface
{
    public class TextBox : BaseControl
    {
        //////////////////////////////////////////////////
        // Data Fields
        //////////////////////////////////////////////////
        string text;
        int IndexToDraw;
        int width;
        float Tick;
        Vector2 position;
        bool selected;
        bool password;

        //////////////////////////////////////////////////
        // Field Properties
        //////////////////////////////////////////////////
        public string Text
        {
            get { return text; }
            set
            {
                // TODO: TRIM
                text = value;

                Vector2 TextDimens = FCanvas.DefFont.MeasureString(text);
                float CharWidth = TextDimens.X / text.Length;
                int MaxChars = Width / (int)CharWidth;


                if (text.Length > MaxChars)
                    IndexToDraw = text.Length - MaxChars;
            }
        }
        public int Width
        {
            get { return width; }
            set { width = value; }
        }
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        public bool Selected
        {
            get { return selected; }
            set { selected = value; }
        }
        public bool Password
        {
            get { return password; }
            set { password = value; }
        }

        //////////////////////////////////////////////////
        // Constructors
        //////////////////////////////////////////////////
        public TextBox()
        {
            IndexToDraw = 0;
            Width = 200;
            Tick = 0.10f;
            Position = Vector2.One;
            Selected = false;
        }

        //////////////////////////////////////////////////
        // BaseControl Methods
        //////////////////////////////////////////////////
        public override void Update(float Delta)
        {
            bool ShiftDown = FInput.KeyDown(Keys.LeftShift) || FInput.KeyDown(Keys.RightShift);

            if (Tick > 0f)
                Tick -= Delta;

            if (Tick <= 0f)
            {
                foreach (Keys Key in FInput.LastPressed)
                {
                    #region Keys
                    switch (Key)
                    {
                        case Keys.D0:
                            if (!ShiftDown)
                                Text += "0";
                            else
                                Text += ")";
                            break;
                        case Keys.D1:
                            if (!ShiftDown)
                                Text += "1";
                            else
                                Text += "!";
                            break;
                        case Keys.D2:
                            if (!ShiftDown)
                                Text += "2";
                            else
                                Text += "@";
                            break;
                        case Keys.D3:
                            if (!ShiftDown)
                                Text += "3";
                            else
                                Text += "#";
                            break;
                        case Keys.D4:
                            if (!ShiftDown)
                                Text += "4";
                            else
                                Text += "$";
                            break;
                        case Keys.D5:
                            if (!ShiftDown)
                                Text += "5";
                            else
                                Text += "%";
                            break;
                        case Keys.D6:
                            if (!ShiftDown)
                                Text += "6";
                            else
                                Text += "^";
                            break;
                        case Keys.D7:
                            if (!ShiftDown)
                                Text += "7";
                            else
                                Text += "&";
                            break;
                        case Keys.D8:
                            if (!ShiftDown)
                                Text += "8";
                            else
                                Text += "*";
                            break;
                        case Keys.D9:
                            if (!ShiftDown)
                                Text += "9";
                            else
                                Text += "(";
                            break;
                        case Keys.NumPad0:
                            Text += "0";
                            break;
                        case Keys.NumPad1:
                            Text += "1";
                            break;
                        case Keys.NumPad2:
                            Text += "2";
                            break;
                        case Keys.NumPad3:
                            Text += "3";
                            break;
                        case Keys.NumPad4:
                            Text += "4";
                            break;
                        case Keys.NumPad5:
                            Text += "5";
                            break;
                        case Keys.NumPad6:
                            Text += "6";
                            break;
                        case Keys.NumPad7:
                            Text += "7";
                            break;
                        case Keys.NumPad8:
                            Text += "8";
                            break;
                        case Keys.NumPad9:
                            Text += "9";
                            break;
                        case Keys.OemPeriod:
                            if (!ShiftDown)
                                Text += ".";
                            else
                                Text += ">";
                            break;
                        case Keys.A:
                            if (!ShiftDown)
                                Text += "a";
                            else
                                Text += "A";
                            break;
                        case Keys.B:
                            if (!ShiftDown)
                                Text += "b";
                            else
                                Text += "B";
                            break;
                        case Keys.C:
                            if (!ShiftDown)
                                Text += "c";
                            else
                                Text += "C";
                            break;
                        case Keys.D:
                            if (!ShiftDown)
                                Text += "d";
                            else
                                Text += "D";
                            break;
                        case Keys.E:
                            if (!ShiftDown)
                                Text += "e";
                            else
                                Text += "E";
                            break;
                        case Keys.F:
                            if (!ShiftDown)
                                Text += "f";
                            else
                                Text += "F";
                            break;
                        case Keys.G:
                            if (!ShiftDown)
                                Text += "g";
                            else
                                Text += "G";
                            break;
                        case Keys.H:
                            if (!ShiftDown)
                                Text += "h";
                            else
                                Text += "H";
                            break;
                        case Keys.I:
                            if (!ShiftDown)
                                Text += "i";
                            else
                                Text += "I";
                            break;
                        case Keys.J:
                            if (!ShiftDown)
                                Text += "j";
                            else
                                Text += "J";
                            break;
                        case Keys.K:
                            if (!ShiftDown)
                                Text += "k";
                            else
                                Text += "K";
                            break;
                        case Keys.L:
                            if (!ShiftDown)
                                Text += "l";
                            else
                                Text += "L";
                            break;
                        case Keys.M:
                            if (!ShiftDown)
                                Text += "m";
                            else
                                Text += "M";
                            break;
                        case Keys.N:
                            if (!ShiftDown)
                                Text += "n";
                            else
                                Text += "N";
                            break;
                        case Keys.O:
                            if (!ShiftDown)
                                Text += "o";
                            else
                                Text += "O";
                            break;
                        case Keys.P:
                            if (!ShiftDown)
                                Text += "p";
                            else
                                Text += "P";
                            break;
                        case Keys.Q:
                            if (!ShiftDown)
                                Text += "q";
                            else
                                Text += "Q";
                            break;
                        case Keys.R:
                            if (!ShiftDown)
                                Text += "r";
                            else
                                Text += "R";
                            break;
                        case Keys.S:
                            if (!ShiftDown)
                                Text += "s";
                            else
                                Text += "S";
                            break;
                        case Keys.T:
                            if (!ShiftDown)
                                Text += "t";
                            else
                                Text += "T";
                            break;
                        case Keys.U:
                            if (!ShiftDown)
                                Text += "u";
                            else
                                Text += "U";
                            break;
                        case Keys.V:
                            if (!ShiftDown)
                                Text += "v";
                            else
                                Text += "V";
                            break;
                        case Keys.W:
                            if (!ShiftDown)
                                Text += "w";
                            else
                                Text += "W";
                            break;
                        case Keys.X:
                            if (!ShiftDown)
                                Text += "x";
                            else
                                Text += "X";
                            break;
                        case Keys.Y:
                            if (!ShiftDown)
                                Text += "y";
                            else
                                Text += "Y";
                            break;
                        case Keys.Z:
                            if (!ShiftDown)
                                Text += "z";
                            else
                                Text += "Z";
                            break;
                        case Keys.Back:
                            if (Text.Length > 0)
                            {
                                Text = Text.Remove(Text.Length - 1, 1);
                            }
                            break;
                        case Keys.Space:
                            Text += ' ';
                            break;
                    }
                    #endregion
                }
            }

            if (FInput.LastPressed.Length > 0 && Tick <= 0f)
                Tick = 0.10f;
        }
        public override void Render(float Delta)
        {
            if (Selected)
                FCanvas.FillRectangle(new Rectangle((int)Position.X - 2, (int)Position.Y - 2, Width + 4, 28), Color.Black);

            FCanvas.FillRectangle(new Rectangle((int)Position.X, (int)Position.Y, Width, 24), Color.White);

            if (Text != null && IndexToDraw < Text.Length)
            {
                if (!Password)
                    FCanvas.DrawString(Text.Substring(IndexToDraw), Position, Color.Black);
                else
                {
                    int NumChars = text.Length - IndexToDraw;
                    string ToDraw = "";

                    for (int i = 0; i < NumChars; ++i)
                        ToDraw += "*";

                    FCanvas.DrawString(ToDraw, Position, Color.Black);
                }
            }
        }
        public override void Dispose()
        {
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////